define(function (require, exports, module) {
    var angular = require('angular');
    require("pagination");
    require("angular-ui");
    require('/public/js/myservice/common/nav');
    var config = require('/public/js/myservice/config');
    require('/public/js/myservice/contract/contract-common');
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀
    require('/public/js/myservice/common/row-toolbar');

    angular.module('Contract', ['ngUI', 'myNav', 'ContractCommonService', 'rowToolbar'])
        .controller('contractListCtrl', ['$scope', 'contractCommonService', 'appservice', function ($scope, contractCommonService, appservice) {
            appservice.name = '明源云客-合同管理';
            $scope.filterDataStatus = [{
                name: '全部', selected: true, value: ''
            }, {
                name: '草稿', value: '0'
            }, {
                name: '未审核', value: '1'
            }, {
                name: '已审核', value: '2'
            }, {
                name: '作废', value: '-1'
            }];
            $scope.filterDataNature = [{
                property_name: '全部', selected: true, property_id: ''
            }];

            //获取合同性质列表
            contractCommonService.getContractNatures(function (data) {
                $.merge($scope.filterDataNature, data);
                $scope.$applyAsync();
            });
            $scope.filterDataType = [{
                name: '全部', selected: true, value: ''
            }, {
                name: '销售合同', value: '1'
            }, {
                name: '服务合同', value: '2'
            }];
            $scope.filterDataCities = [{
                areaName: '全部', selected: true, areaId: ''
            }];
            //获取合同区域列表
            contractCommonService.getContractAreas(function (data) {
                $.merge($scope.filterDataCities, data);
                $scope.$applyAsync();
            });

            //合同列表
            $scope.contracts = [];

            $scope.changeStatus = function (status) {
                $scope.query.execute();
            }
            $scope.changeNature = function (nature) {
                $scope.query.execute();
            }
            $scope.changeType = function (itemType) {
                $scope.query.execute();
            }
            $scope.changeCity = function (city) {
                $scope.query.execute();
            }

            //操作
            //提交复核
            $scope.submitVerify = function (contract) {
                util.confirm('确定要提交复核吗?', {
                    ok: function () {
                        var post_data = {contractId: contract.contract_id};
                        var url = api_prefix + "/contract/submit-review";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            context: this,
                            data: post_data,
                            success: function (json) {
                                if (json.retCode === "0") {
                                    contract.contract_status = 1;
                                    $scope.$applyAsync();

                                } else {
                                    util.alert("提交复核失败：" + json.errMsg);
                                }
                            }
                        });
                    },
                    cancel: function () {
                    }
                });
            }
            //取消复核
            $scope.cancelVerify = function (contract) {
                util.confirm('确定要撤销复核吗?', {
                    ok: function () {
                        var post_data = {contractId: contract.contract_id};
                        var url = api_prefix + "/contract/cancel-review";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            context: this,
                            data: post_data,
                            success: function (json) {
                                if (json.retCode === "0") {
                                    contract.contract_status = 0;
                                    $scope.$applyAsync();

                                } else {
                                    util.alert("撤销复核失败：" + json.errMsg);
                                }
                            }
                        });
                    },
                    cancel: function () {
                    }
                });
            };

            //确认复核
            $scope.confirmVerify = function (contract) {
                util.confirm('确定要复核此合同吗?', {
                    ok: function () {
                        var post_data = {contractId: contract.contract_id};
                        var url = api_prefix + "/contract/audit-review";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            context: this,
                            data: post_data,
                            success: function (json) {
                                if (json.retCode === "0") {
                                    contract.contract_status = 2;
                                    $scope.$applyAsync();

                                } else {
                                    util.alert("合同复核失败：" + json.errMsg);
                                }
                            }
                        });
                    },
                    cancel: function () {
                    }
                });
            }

            //作废
            $scope.cancelContract = function (contract) {
                util.confirm('确定要作废合同吗?', {
                    ok: function () {
                        var post_data = {contractId: contract.contract_id};
                        var url = api_prefix + "/contract/cancel-contract";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            context: this,
                            data: post_data,
                            success: function (json) {
                                if (json.retCode === "0") {
                                    contract.is_cancelled = 1;
                                    $scope.$applyAsync();

                                } else {
                                    util.alert("作废合同失败：" + json.errMsg);
                                }
                            }
                        });
                    },
                    cancel: function () {
                    }
                });
            }
            //查询条件
            $scope.getQueryParams = function (page) {
                var parms = {keyword: $scope.query.keyword};
                parms.pageIndex = page || 1;
                parms.pageSize = config.pager().pageSize;

                //收集过滤条件
                //合同状态
                filterStatus = $.grep($scope.filterDataStatus, function (o, n) {
                    return o.selected == true;
                });
                if (filterStatus.length) {
                    parms.status = filterStatus[0].value;
                }
                //合同性质
                filterNature = $.grep($scope.filterDataNature, function (o, n) {
                    return o.selected == true;
                })
                if (filterNature.length) {
                    parms.property = filterNature[0].property_id;
                }
                //城市区域
                filterCity = $.grep($scope.filterDataCities, function (o, n) {
                    return o.selected == true;
                })
                if (filterCity.length) {
                    parms.areaId = filterCity[0].areaId;
                }
                return parms;
            };
            //查询
            $scope.query = {
                keyword: '',
                execute: function (page) {
                    var parms = $scope.getQueryParams(page);

                    //console.log('search' + JSON.stringify(parms))
                    var get_data = parms;
                    var url = api_prefix + "/contract/get-contract-list";

                    util.myAjax({
                        url: url,
                        type: "GET",
                        context: this,
                        data: get_data,
                        success: function (json) {
                            if (json.retCode === "0") {
                                $scope.contracts = json.data.items;
                                $scope.$applyAsync();

                                $scope.startPageIndex = (parms.pageIndex - 1) * 10;
                                $('#pagination').pagination(parseInt(json.data.total), {
                                    current_page: parms.pageIndex - 1,
                                    num_edge_entries: 1, //边缘页数
                                    num_display_entries: 4, //主体页数
                                    callback: function (page_index, jq) {
                                        //page_index += 1; // 分页控件从 0 开始，后台从 1 开始
                                        $scope.startPageIndex = (page_index - 1) * 10;
                                        $scope.query.execute(page_index);
                                        //if (isPagination === true && page_index !== service.pageIndex) {
                                        /*service.pageIndex = page_index;
                                         service.doQuery(true);*/
                                        //}
                                    },
                                    items_per_page: config.pager().pageSize //每页显示1项
                                });

                            } else {
                                this.showTip("获取合同列表失败：" + json.errMsg);
                            }
                        }
                    });
                }
            };

            /**
             * 导出
             */
            $scope.export = function () {
                util.confirm('确定要导出数据吗？', {
                    ok: function () {
                        var parms = $scope.getQueryParams();
                        delete parms.pageIndex;
                        delete parms.pageSize;
                        delete parms.sortBy;
                        delete parms.sortAsc
                        var url = api_prefix + "/contract/export-contract&XDEBUG_SESSION_START=netbeans-xdebug";
                        util.download(url, parms);
                    }
                });
            };
            $scope.query.execute();
        }]);
});